<?php

	header("Content-Type:text/html; charset=utf-8");
 	 
	$file_name = $_FILES['Filedata']['name']; //取得檔名
	$file_size = number_format(($_FILES['Filedata']['size']/1024), 1, '.', ''); //取得檔案大小
	//$allowSubName = $_POST['allowSubName']; //允許檔案格式
	$allowMaxSize = 1024;//$_POST['maxSize']; //允許上傳大小
	//$subn_array = explode(",",$allowSubName);//分割允許上傳副檔名
	
	$checkSubName = "";
	$checkSize = "";
	$checkmsg = "";
	
	$fn_array=explode(".",$file_name);//分割檔名
	$mainName = $fn_array[0];//檔名
	$subName = $fn_array[1];//副檔名	
	/*
	//判斷檔案格式
	foreach($subn_array as $index => $value){
		if($subName == $value){			
			$checkSubName ="ok";
						
			break;
		}else{
			$checkSubName ="檔案格式錯誤,請上傳圖片檔檔案";
		
		}
	}*/
 
	//判斷上傳檔案
	if($file_size <= $allowMaxSize){		
		$checkmsg = "ok";
	}else{
		$checkmsg = "檔案超過1MB限制,請重新上傳小於1MB檔案";
	} 
	

	if ($checkmsg == "ok") {
		$upFloder = $_GET['floder'];
		if($upFloder != ""){
			$upload_dir = $upFloder.'/';
		}
			
		//中文檔名處理
		if (mb_strlen($mainName,"Big5") != strlen($mainName))
		{
			$mainName = "undefine".date("ymdHi");//重新命名=檔名+日期
			$file_name = sprintf("%s.%s",$mainName,$subName);//組合檔名
		}	
		$upload_file = $upload_dir . basename($file_name);
		
		
		//檔名重覆處理
		$x=1;
		while(file_exists($upload_file)){
			$file_name = sprintf("%s_%d.%s",$mainName ,$x++ ,$subName);//組合檔名
			$upload_file = $upload_dir . basename($file_name);
		}
		
		$temploadfile = $_FILES['Filedata']['tmp_name'];
		/*
		$src = imagecreatefromjpeg($temploadfile);
		
		// get the source image's widht and hight
		$src_w = imagesx($src);
		$src_h = imagesy($src);
		 
		// assign thumbnail's widht and hight
		if($src_w > $src_h){
			$thumb_w = $_GET["w"];
			$thumb_h = intval(($src_h / $src_w)* $thumb_w);
		}else{
			$thumb_h = $_GET["h"];
			$thumb_w = intval(($src_w / $src_h) * $thumb_h);
		}
 
		// if you are using GD 1.6.x, please use imagecreate()
		$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
		 
		// start resize
		imagecopyresized($thumb, $src, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_w,    $src_h);
		
		$result  = imagejpeg($thumb, $upload_file);
		*/
		$result = move_uploaded_file($temploadfile , $upload_file);
	}
	 
	if ($result != null){ //判斷上傳結果
        echo "{success:true,msg:'照片已上傳成功!',url:'$upload_file'}";
     }else{ 
    	echo "{failure:true,msg:'照片上傳失敗! 訊息:".$checkmsg ."'}";
	}
?>
    
 